import { generatorDynamicRouter } from '@/utils/generator-routes'
import { constantRoutes } from '@/router'

const state = {
  asyncRouters: [],
  defaultRouters: constantRoutes
}

const mutations = {
  SET_ASYNC_ROUTER: (state, routes) => {
    state.asyncRouters = constantRoutes.concat(routes)
  }
}

const actions = {
  // get routes
  asyncRouterList({ commit }) {
    return new Promise((resolve, reject) => {
      // 获取路由动态路由数据
      generatorDynamicRouter().then(routers => {
        // 保存路由地址到state仓库中
        commit('SET_ASYNC_ROUTER', routers)
        resolve(routers)
      }).catch((err) => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

